<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="状态">
              <!-- 10待审核 11通过 12拒绝 -->
              <a-select v-model="queryParam.auditStatus" allowClear>
                <a-select-option value="10">待审核</a-select-option>
                <a-select-option value="11">通过</a-select-option>
                <a-select-option value="12">拒绝</a-select-option>
                <a-select-option value="20">待发货</a-select-option>
                <a-select-option value="21">已发货</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="所属机构">
              <a-select
                allowClear
                defaultValue="1"
                v-model="queryParam.orgId"
                showSearch
                :filter-option="filterOption"
                :not-found-content="null"
              >
                <a-select-option v-for="item in orgList" :key="item.id" :value="item.id">
                  {{ item.departName }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="调入门店">
              <a-input placeholder="" v-model="queryParam.inShopName" allowClear></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="调出门店">
              <a-input placeholder="" v-model="queryParam.outShopName" allowClear></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="sku编码">
              <a-input placeholder="" v-model="queryParam.itemSku" allowClear></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="单据号">
              <a-input placeholder="" v-model="queryParam.billNo" allowClear></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item>
              <template #label>
                <a-select v-model="queryParam.dateColumn">
                  <a-select-option value="applyTime">申请时间</a-select-option>
                  <a-select-option value="deliveryTime">发货时间</a-select-option>
                </a-select>
              </template>
              <a-range-picker
                :ranges="{ 今天: [moment(), moment()], 本月: [moment().startOf('month'), moment().endOf('month')] }"
                v-model="orderDateRange"
                @change="handleChange"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button
        v-if="queryParam.auditStatus == 10"
        style="margin-left: 8px"
        type="primary"
        :disabled="selectedRowKeys.length <= 0"
        @click="handleBatchAudit"
      >
        批量审核
      </a-button>
      <a-button style="margin-left: 8px" @click="handleStatus('作废',14)" type="primary" v-if="queryParam.auditStatus == 20">作废</a-button>
      <a-button style="margin-left: 8px" @click="handleRule" icon="setting" type="primary">设置规则</a-button>
      
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleView(record)">详情</a>
        </span>
        <!--10待审核 11通过 12拒绝 13取消 20待发货 21已发货/待签收 22已签收-->
        <span slot="bizStatus" slot-scope="text">
          <a-tag color="orange" v-if="text === 10">待审核</a-tag>
          <a-tag color="blue" v-if="text === 11">审核通过</a-tag>
          <a-tag color="red" v-if="text === 12">审核拒绝</a-tag>
          <a-tag v-if="text === 13">取消</a-tag>
          <a-tag v-if="text === 14">作废</a-tag>
          <a-tag color="blue" v-if="text === 20">待发货</a-tag>
          <a-tag color="green" v-if="text === 21">已发货</a-tag>
        </span>
        <div slot="deliveryInfo" slot-scope="text,record" style="text-align:left;">
          <template v-if="true && record.deliveryTime">
            {{record.deliveryWay === 1 ? '快递':'自提'}} , {{record.deliveryRemarks}}
          </template>
        </div>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->

    <!-- 审核-->
    <a-modal
      title="批量审核"
      :width="600"
      :visible="auditVisible"
      :confirmLoading="auditConfirmLoading"
      @ok="handleAuditSubmit"
      @cancel="handleAuditCancel"
      cancelText="关闭"
    >
      <a-form-model ref="auditForm" :model="model" :rules="validatorRules1">
        <a-form-model-item label="审核意见" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bizStatus">
          <a-select v-model="model.bizStatus">
            <a-select-option value="11">通过</a-select-option>
            <a-select-option value="12">拒绝</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="审核备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
          <a-input placeholder="" v-model="model.remark" />
        </a-form-model-item>
        <a-form-model-item label="审核提示" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tips">
          <span>选择待审批的数据会进行审批操作</span>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import PosShopGoodsStockTransferModal from './modules/PosShopGoodsStockTransferModal'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { postAction } from '@/api/manage'
import moment from 'moment'
import { Modal } from 'ant-design-vue'

export default {
  name: 'PosShopGoodsStockTransferList',
  mixins: [JeecgListMixin],
  components: {
    PosShopGoodsStockTransferModal,
  },
  data() {
    return {
      description: '调拨单管理页面',
      disableMixinCreated: true,
      orgList: [],
      orderDateRange: [],
      model: {},
      moment,
      auditVisible: false,
      auditConfirmLoading: false,
      validatorRules1: {
        bizStatus: [{ required: true, message: '必填项' }],
        remark: [{ required: true, message: '必填项' }],
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      // 表头
      columns: [
        {
          title: '单据号',
          align: 'center',
          dataIndex: 'billNo',
        },
        {
          title: '所属机构',
          align: 'center',
          dataIndex: 'orgName',
        },
        {
          title: '调出门店',
          align: 'center',
          dataIndex: 'outShopName',
        },

        {
          title: '调入门店',
          align: 'center',
          dataIndex: 'inShopName',
        },
        
        {
          title: 'SKU编码',
          align: 'center',
          dataIndex: 'itemSku',
        },
        {
          title: '商品名称',
          align: 'center',
          dataIndex: 'itemName',
          customRender: (text, record) => {
            return text + ' ' + record.itemColor + '*' + record.itemSize
          },
        },
        {
          title: '数量',
          align: 'center',
          sorter: true,
          dataIndex: 'qty',
        },
        {
          title: '零售价',
          align: 'center',
          sorter: true,
          dataIndex: 'markPrice',
        },
        {
          title: '品牌',
          align: 'center',
          dataIndex: 'brandName',
        },
        {
          title: '申请人',
          align: 'center',
          dataIndex: 'operUserName',
        },
        {
          title: '申请时间',
          align: 'center',
          sorter: true,
          dataIndex: 'applyTime',
        },
        {
          title: '单据状态',
          align: 'center',
          dataIndex: 'bizStatus',
          scopedSlots: { customRender: 'bizStatus' },
        },
        {
          title: '发货时间',
          align: 'center',
          dataIndex: 'deliveryTime',
        },
        {
          title: '发货信息',
          align: 'center',
          dataIndex: 'deliveryInfo',
          scopedSlots: { customRender: 'deliveryInfo' },
        },
      ],
      url: {
        list: '/stock/transfer/list',
        batchChangeStatus: '/stock/transfer/batchChangeStatus',
        exportXlsUrl: '/stock/transfer/exportXls',
        shopListUrl: '/sys/sysDepart/getMyDepartList',
      },
    }
  },
  computed: {},
  created() {
    this.$set(this.queryParam, 'auditStatus', '10')
    this.$set(this.queryParam, 'dateColumn', 'deliveryTime')
    this.loadShopList()
    this.loadData(1)
  },
  methods: {
    handleRule() {
      this.$router.push({ path: '/transfer/conf' })
    },
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    loadShopList() {
      this.orgList = []
      postAction(this.url.shopListUrl + '?type=2').then((res) => {
        if (res.success) {
          this.orgList = res.result
        }
      })
    },
    handleChange(date, dateString) {
      this.queryParam.beginTime = dateString[0]
      this.queryParam.endTime = dateString[1]
    },
    handleBatchAudit() {
      this.$set(this.model, 'bizStatus', null)
      this.auditVisible = true
    },
    handleView(record) {
      this.model = Object.assign({}, record)
      this.auditVisible = true
    },
    handleStatus(title,bizStatus){
      if (this.selectedRowKeys.length <= 0) {
          this.$message.warning('请选择一条记录！')
          return
      }
      let that = this
      Modal.confirm({
        title: '确定'+title+'?' ,
        okText: '确定',
        okType: 'warning',
        cancelText: '取消',
        onOk() {
          console.log('OK')
          that.doChangeStatus(title,bizStatus)
        },
        onCancel() {
          console.log('Cancel')
        },
      })


    },
    buildSelectIds(){
      var ids = ''
      let billIds = []
      for(let row of this.selectedRowKeys){
        billIds.push(row.split('_')[0])
      }
      console.log('--billIds--', billIds)

      let idSet = Array.from(new Set(billIds))

      console.log('--not repeat idSet--', idSet)

      for (var a = 0; a < idSet.length; a++) {
        ids += idSet[a] + ','
      }
      return ids
    },
    doChangeStatus(remark,bizStatus){
      var ids = this.buildSelectIds()
      let params = {
        ids: ids,
        status: bizStatus,
        remark: remark,
      }
      let that = this
      postAction(that.url.batchChangeStatus, params)
        .then((res) => {
          if (res.success) {
            that.$message.success(res.message)
          } else {
            that.$message.warning(res.message)
          }
        })
        .finally(() => {
          that.onClearSelected()
          that.loadData(1)
        })
    },
    handleAuditSubmit() {
      console.log(this.model)
      const that = this
      // 触发表单验证
      this.$refs.auditForm.validate((valid) => {
        if (valid) {
          if (this.selectedRowKeys.length <= 0) {
            this.$message.warning('请选择一条记录！')
            return
          } else {
            var ids = this.buildSelectIds()
            this.auditConfirmLoading = true
            let params = {
              ids: ids,
              status: that.model.bizStatus,
              remark: that.model.remark,
            }
            postAction(that.url.batchChangeStatus, params)
              .then((res) => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.loadData()
                  that.onClearSelected()
                } else {
                  that.$message.warning(res.message)
                }
              })
              .finally(() => {
                that.auditConfirmLoading = false
                that.handleAuditCancel()
              })
          }
        } else {
          return false
        }
      })
    },
    handleAuditCancel() {
      this.model = {}
      this.auditVisible = false
      this.$refs.auditForm.clearValidate()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>